<?php
/**
 * 
 * @author bqd
 *
 */
class HasRelatedImagesBehavior extends ModelBehavior {
	var $options = array();
	var $association = array(
		'RelatedImage' => array('className' => 'RelatedImage',
								'foreignKey' => 'foreign_key',
								'dependent' => true,
								'conditions' => '',
								'fields' => '',
								'order' => 'priority desc',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			)
	);
    function setup(&$model, $settings = array()) {
        $default = array(
			'image_field'	=> 'image',
		);

		if (!isset($this->__settings[$model->alias])) {
			$this->__settings[$model->alias] = $default;
		}

		$this->__settings[$model->alias] = am($this->__settings[$model->alias], $settings);
		//$this->__bindModel($model);
    }
	function __bindModel(&$model) {
		$this->association['RelatedImage']['conditions']	= array('model' => $model->name);
		$model->bindModel(array(
			'hasMany'	=>	$this->association
		), false);
	}
	    
    function afterFind(&$model, $results, $primary) {
        //debug($results); exit;
        // Truy v?n v?n d? li?u c?a related image và n?p vào cho results
        $conditions = array(
            'RelatedImage.model'         => $model->name,
            'RelatedImage.foreign_key'   => $model->id
        );

        $data = ClassRegistry::init('related_images.RelatedImage')->find('all', array(
            'conditions'    => $conditions,
			'order' => 'priority desc',
        ));
        
        $image_data = array();  $j = 0;
        foreach($data as $item){
            $image_data[$j] = $item['RelatedImage'];
            $j++;
        } 

        //debug($data); exit;
        if (isset($results[$model->name])) {
            $results['RelatedImage'] = $image_data;
        } else if (isset($results[0])) {
            for ($i = 0; $i < count($results); $i++) {
                $results[$i]['RelatedImage'] = $image_data;
            }
        }
        //debug($results);exit;
        return $results;
    }
    
	function beforeDelete(&$model, $cascade) {
		if (!isset($model->configs)) {
			$model->configs = $model->loadConfig();
		}
		
		// Lấy tất cả các related images liên quan
		$related_images = ClassRegistry::init('related_images.RelatedImage')->find('list', array(
			'conditions'	=> array(
				'RelatedImage.model'		=> $model->name,
				'RelatedImage.foreign_key'	=> $model->id
			),
			'contain'	=> array(),
			'fields'	=> array('RelatedImage.id', 'RelatedImage.image_name')
		));
		
		foreach ($related_images as $image_name) {
			if (is_file($model->configs['RelatedImages']['image']['path'].$image_name)) {
				@unlink($model->configs['RelatedImages']['image']['path'].$image_name);
			}
			
			if ($model->configs['RelatedImages']['thumbnail']['thumbnail']) {
				// Xử lý để lấy tên thumbnail
				$ext = strrchr($image_name, ".");
				$pos = strrpos($image_name, ".");
				$filename = substr($image_name,0,$pos);
				$thumbnail_name = $filename."_thumbnail".$ext;
				@unlink($model->configs['RelatedImages']['thumbnail']['path'].$thumbnail_name);
			}
		}
	}
}
?>